package com.liu.service.impl;

import com.github.pagehelper.PageHelper;
import com.liu.dao.BookInfoMapper;
import com.liu.dao.BookMapper;
import com.liu.dao.BookUseRecordMapper;
import com.liu.dao.ReaderMapper;
import com.liu.model.Book;
import com.liu.model.BookInfo;
import com.liu.model.BookUseRecord;
import com.liu.service.BookUseRecordService;
import com.liu.tools.MyConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Reader;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by liu on 2018/3/26.
 */
@Service(value = "bookUseRecordService")
public class BookUseRecordServiceImpl implements BookUseRecordService{

    @Resource
    BookUseRecordMapper bookUseRecordDao;

    @Resource
    BookInfoMapper bookInfoDao;

    @Resource
    BookMapper bookDao;

    @Override
    public Boolean add(BookUseRecord bookUseRecord) {
        bookUseRecord.setCreateTime(new Date());
        bookUseRecord.setStatus(MyConstant.BOOKINFO_BORRWOING);
        Book book = new Book();
        if (bookUseRecord.getBookInfoId() != null) {
            try {
                book = bookDao.findByBookInfoId(bookUseRecord.getBookInfoId());
                bookUseRecord.setBookId(book.getId());
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            book = bookDao.findById(bookUseRecord.getBookId());
        }

        book.setUpdateTime(new Date());
        book.setBorrowStatus(MyConstant.BOOKINFO_BORRWOING);

        bookDao.edit(book);
        Boolean flag = bookUseRecordDao.save(bookUseRecord);
        return flag;
    }


    @Override
    public Boolean borrowConfirm(String id) {
        BookUseRecord bookUseRecord = bookUseRecordDao.selectByPrimaryKey(id);
        Book book = bookDao.findById(bookUseRecord.getBookId());
        book.setUpdateTime(new Date());
        bookUseRecord.setBorrowTime(new Date());
        book.setBorrowStatus(MyConstant.BOOKINFO_BORROW);
        bookUseRecord.setStatus(MyConstant.BOOKINFO_BORROW);
        BookInfo bookInfo = bookInfoDao.findById(book.getBookInfoId());
        int borrowNumber = bookInfo.getBorrowNumber();
        int number = borrowNumber+1;
        bookInfo.setBorrowNumber(number);
        bookInfoDao.edit(bookInfo);
        bookDao.edit(book);
        Boolean flag = bookUseRecordDao.edit(bookUseRecord);
        return flag;
    }

    @Override
    public Boolean returnById(String id) {
        BookUseRecord bookUseRecord = bookUseRecordDao.selectByPrimaryKey(id);
        bookUseRecord.setReturnTime(new Date());
        bookUseRecord.setUpdateTime(new Date());
        bookUseRecord.setStatus(MyConstant.BOOKINFO_RETURN);

        Book book = bookDao.findById(bookUseRecord.getBookId());
        book.setUpdateTime(new Date());
        book.setBorrowStatus(MyConstant.BOOKINFO_RETURN);
        bookDao.edit(book);
        boolean flag = bookUseRecordDao.edit(bookUseRecord);
        return flag;
    }

    @Override
    public Boolean edit(BookUseRecord bookUseRecord) {
        Boolean flag = bookUseRecordDao.edit(bookUseRecord);
        return flag;
    }

    @Override
    public Boolean deleteById(String id) {
        Boolean flag = bookUseRecordDao.deleteById(id);
        return flag;
    }

    @Override
    public BookUseRecord findById(String id) {
        return bookUseRecordDao.selectByPrimaryKey(id);
    }

    /*
   * 这个方法中用到了我们开头配置依赖的分页插件pagehelper
   * 很简单，只需要在service层传入参数，然后将参数传递给一个插件的一个静态方法即可；
   * pageNum 开始页数
   * pageSize 每页显示的数据条数
   * */
    @Override
    public List<BookUseRecord> findAll(int pageNum, int pageSize, Map<String, Object> queryMap) {
        //将参数传给这个方法就可以实现物理分页了，非常简单。
        PageHelper.startPage(pageNum, pageSize);
        List<BookUseRecord> list = bookUseRecordDao.selectAll(queryMap);

        return list;
    }
}
